package com.wandian.news.mvvm.wxtop.wxtopactivity;

import android.os.Bundle;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.mylib.libcore.adapter.BasePagerAdapter;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.mvvm.BaseMVVMActivity;
import com.mylib.libcore.utils.StatusBarUtils;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.news.R;
import com.wandian.news.databinding.NewsActivityWxTopBinding;
import com.wandian.news.mvvm.wxtop.wxfeaturedpage.WXFeaturedPageFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @date 2019/12/18-14:28
 * @description WXTopActivity
 **/
@Route(path = RouterConfig.ROUTER_ACTIVITY_WX_TOP)
public class WXTopActivity extends BaseMVVMActivity<WXTopActivityViewModel>  {
    private NewsActivityWxTopBinding dataBinding;
    private BasePagerAdapter myAdapter;
    private List<String> titleList=new ArrayList<>();
    private List<Fragment> fragments = new ArrayList<>();
    @Override
    public void init(Bundle savedInstanceState) {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.news_activity_wx_top);
        dataBinding.setLifecycleOwner(this);
        dataBinding.setViewModel(mViewModel);
        StatusBarUtils.with(this,false)
                .setIsActionBar(false)
                .clearActionBarShadow()
                .setDrawable(getResources().getDrawable(R.drawable.sys_color))
                .init();
        initViewData();

    }

    private void initViewData(){
        dataBinding.bindTransparentToolBar.toolBarTvTitle.setText("微文精选");
        dataBinding.bindTransparentToolBar.toolBarIvBack.setOnClickListener(v -> finish());
        titleList.clear();
        fragments.clear();
        titleList.add("热文");
        titleList.add("精选");
        titleList.add("搞笑");
        titleList.add("话题");
        titleList.add("古通");
        fragments.add(WXFeaturedPageFragment.newInstance("热文"));
        fragments.add(WXFeaturedPageFragment.newInstance("精选"));
        fragments.add(WXFeaturedPageFragment.newInstance("段子"));
        fragments.add(WXFeaturedPageFragment.newInstance("话题"));
        fragments.add(WXFeaturedPageFragment.newInstance("古通"));
        initTabPage();
    }

    //初始化tabpage
    private void initTabPage() {
        myAdapter = new BasePagerAdapter(getSupportFragmentManager(), fragments,titleList);
        if(UIUtils.isEmpty(dataBinding.viewPager.getAdapter())){
            dataBinding.viewPager.setAdapter(myAdapter);
            dataBinding.viewPager.setOffscreenPageLimit(titleList.size());
            dataBinding.tabLayout.setViewPager(dataBinding.viewPager);
        }
        if(!UIUtils.isEmpty(myAdapter)){
            myAdapter.notifyDataSetChanged();
        }
        //预加载数量
        dataBinding.viewPager.setOffscreenPageLimit(1);
        dataBinding.tabLayout.setViewPager(dataBinding.viewPager);
        dataBinding.tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
            }

            @Override
            public void onTabReselect(int position) {
            }
        });
        dataBinding.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }
}
